package cn.vshare.service.impl;

import cn.vshare.dao.LogDao;
import cn.vshare.entity.Log;
import cn.vshare.service.LogService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service("logService")
public class LogServiceImpl implements LogService {

    @Autowired
    private LogDao logDao;

    @Override
    public Page<Log> getAllLogs(int currPage, int pageSize) {
        Page<Log> logPage = new Page<Log>();
        int totalRows = logDao.countAllLog(); //获取总行数
        int totalPages = totalRows % pageSize == 0 ? totalRows / pageSize : totalRows / pageSize + 1;  //获取总页数
        logPage.setPageSize(pageSize);
        logPage.setTotal(totalRows);
        logPage.setPageNum(totalPages);
        logPage = PageHelper.startPage(currPage, pageSize); //开始执行分页
        List<Log> logList = logDao.getAllLog(); //获取查询结果
        return logPage;
    }

    @Override
    public List<Log> exportAllLogs() {
        return logDao.getAllLog();
    }

    @Override
    public void delLog(Log log) {
        logDao.delLog(log);
    }

    @Override
    public Page<Log> queryLogs(String startTime, String endTime, int currPage, int pageSize, String message) {
        Page<Log> logPage = new Page<Log>();
        Map<String, Object> map = new HashMap<String, Object>();
        if (startTime.trim().equals("") || startTime == null) {
            map.put("startTime", null);
        } else {
            map.put("startTime", startTime);
        }

        if (endTime.trim().equals("") || endTime == null) {
            map.put("endTime", null);
        } else {
            map.put("endTime", endTime);
        }
        map.put("message", message);
        int totalRows = logDao.countLogList(map); //获取总行数
        int totalPages = totalRows % pageSize == 0 ? totalRows / pageSize : totalRows / pageSize + 1; //计算总页数
        logPage.setTotal(totalRows);
        logPage.setPageNum(totalPages);
        logPage.setPageSize(pageSize);
        logPage = PageHelper.startPage(currPage, pageSize);
        List<Log> logList = logDao.getLogList(map);
        return logPage;
    }

    @Override
    public int countLoginTimesByTime(Map<String, Object> map) {
        return logDao.countLoginTimes(map);
    }

    public LogDao getLogDao() {
        return logDao;
    }

    public void setLogDao(LogDao logDao) {
        this.logDao = logDao;
    }
}
